package com.jshop.seckill.controller;

import com.Jshop.common.utils.DateUtil;
import com.github.pagehelper.PageInfo;
import com.jshop.seckill.filter.LoginInterceptor;
import com.jshop.seckill.pojo.Order;
import com.jshop.seckill.pojo.SeckillGoods;
import com.jshop.seckill.pojo.UserInfo;
import com.jshop.seckill.service.SeckillGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api("订单服务接口")
@RestController
@RequestMapping("/seckill/goods")
@CrossOrigin
public class SeckillGoodsController {

    @Resource
    private SeckillGoodsService seckillGoodsService;

    /**
     * 根据当前时间  获取获取秒杀时间菜单
     * @return
     */
    @GetMapping("/menus")
    @ApiOperation(value = "根据当前时间，获取获取秒杀时间菜单", notes = "根据当前时间，获取获取秒杀时间菜单")
    public List<Date> datemenus() {
        return DateUtil.getDateMenus();
    }

    /**
     * 根据时间段(2019090516) 查询该时间段的所有的秒杀的商品
     */
    @GetMapping("/list")
    @ApiOperation(value = "输入时间段查询秒杀商品", notes = "输入时间段查询秒杀商品")
    public ResponseEntity<List<SeckillGoods>> list(@RequestParam("time") String time){
        return ResponseEntity.ok(seckillGoodsService.list(time));
    }

    /**
     * @Description 查询所有秒杀商品 DB角度
     **/
    @PostMapping(value = "/search/{page}/{size}")
    @ApiOperation(value = "查询所有秒杀商品", notes = "查询所有秒杀商品")
    public ResponseEntity<PageInfo<SeckillGoods>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用SeckillGoodsService实现分页条件查询SeckillGoods
        PageInfo<SeckillGoods> pageInfo = seckillGoodsService.findPage(page, size);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 更新商品秒杀信息
     **/
    @PutMapping(value = "/{id}")
    @ApiOperation(value = "更新秒杀商品", notes = "更新")
    public ResponseEntity<Void> update(@RequestBody SeckillGoods seckillGoods) {
        //调用SeckillGoodsService实现修改SeckillGoods
        seckillGoodsService.update(seckillGoods);
        return ResponseEntity.ok().build();
    }

    /***
     * 新增秒杀商品
     */
    @PostMapping
    @ApiOperation(value = "新增秒杀商品", notes = "新增秒杀商品")
    public ResponseEntity<Void> add(@RequestBody SeckillGoods seckillGoods) {
        //调用SeckillGoodsService实现添加SeckillGoods
        seckillGoodsService.add(seckillGoods);
        return ResponseEntity.ok().build();
    }


    /***
     * 秒杀下单
     */
    @ApiOperation(value = "秒杀", notes = "秒杀")
    @PostMapping(value = "/add")
    public ResponseEntity<Void> add(@RequestParam(value = "time") String time,
                                    @RequestParam(value = "id") Long id){
        try {
            //获取登录用户
            UserInfo user = LoginInterceptor.getLoginUser();

            //调用Service增加订单
            Boolean bo = seckillGoodsService.add(id, time, user);

            if(bo){
                //抢单成功
                return ResponseEntity.ok().build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }


}
